﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using OmegaEngine;

namespace OmegaEngine
{
    public class GUIManager : Component, I2DComponent
    {
        // Boolean value to check for input. If turned off, the GUI will not check for user input such as mouse clicks.
        // This will speed up things that don't need to check for input, such as a simple HUD
        bool checkInput = false;

        // List of 2D components to have
        public ComponentCollection Components;

        public Rectangle Rectangle { get; set; }

        // Constructors
        public GUIManager() : base()
        {
            // Setup our component collection
            Components = new ComponentCollection(this.Parent);

            this.Initialized = true;
        }

        // Constructors
        public GUIManager(GameScreen Parent) : base(Parent)
        {
            // Setup our component collection
            Components = new ComponentCollection(this.Parent);

            this.Initialized = true;
        }

        // Function to add components to the game screen
        public void AddComponent(Component Component)
        {
            if (!Components.Contains(Component))
            {
                Components.Add(Component);
                Component.Parent = this.Parent;
            }
        }

        // Allows for components to be retried with a [] index, ie:
        // Component c = gameScreenInstance['Component1'];
        public Component this[String Name]
        {
            get
            {
                foreach (Component component in Components)
                    if (component.Name == Name)
                        return component;

                return null;
            }
        }

        // Remove component from GameScreen by name
        public void RemoveComponent(string Name)
        {
            Component c = this[Name];
            RemoveComponent(c);
        }

        // Remove component from GameScreen by match component
        public void RemoveComponent(Component Component)
        {
            if (Component != null && Components.Contains(Component))
            {
                Components.Remove(Component);
                Component.Parent = null;
            }
        }

        // Update the screen and child COmponents
        public override void Update()
        {
            // Create a temporary list so that we don't crash if a component is added to the collection while updating
            List<Component> updating = new List<Component>();

            // Populate the temporary list
            foreach (Component c in Components)
                updating.Add(c);

            // Update all components that have been initialized
            foreach (Component Component in updating)
                if (Component.Initialized)
                    Component.Update();
        }

        // Draw
        public override void Draw()
        {
            // Create a temporary list so that we don't crash if a component is added to the collection while updating
            List<Component> updating = new List<Component>();

            // Populate the temporary list
            foreach (Component c in Components)
                updating.Add(c);

            // Update all components that have been initialized
            foreach (Component Component in updating)
                if (Component.Initialized)
                    Component.Draw();
        }

        // Disables the GameScreen
        public virtual void Disable()
        {
            // Clear out our components
            Components.Clear();

            base.DisableComponent();
        }

        // Override ToString() to return our name
        public override string ToString()
        {
            return Name;
        }
    }
}
